﻿package com.badibadi.activity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

import com.badibadi.adapter.MyOtherSpaceAdapter;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MyOtherSpaceActivity extends BaseActivity implements Callback,PlatformActionListener{

	private ListView mlistView;
	private MyOtherSpaceAdapter myOtherSpaceAdapter;
	List<Integer> list;
	private static final int MSG_AUTH_CANCEL = 3;
	private static final int MSG_AUTH_COMPLETE = 5;
	private static final int MSG_AUTH_ERROR = 4;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		ShareSDK.initSDK(MyOtherSpaceActivity.this);
		setContentView(R.layout.my_other_space);

		init();
		
	}

	private void init() {
		// TODO Auto-generated method stub
this.findViewById(R.id.return_btn).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		initList();
		myOtherSpaceAdapter = new MyOtherSpaceAdapter(
				MyOtherSpaceActivity.this, list);
		mlistView = (ListView) this.findViewById(R.id.my_other_space_listView);
		mlistView.setAdapter(myOtherSpaceAdapter);

		mlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub
				// if(position==)
                
//				Utils.showMessage(MyOtherSpaceActivity.this, "成功"+position);
				souquan(position);

			}


		});
	}
	protected void souquan(int position) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			Platform sinaweibo = ShareSDK.getPlatform(
					MyOtherSpaceActivity.this, SinaWeibo.NAME);
			if (sinaweibo.isValid()) {

			} else {
				sinaweibo.setPlatformActionListener(this);
				sinaweibo.SSOSetting(true);
				sinaweibo.showUser(null);
			}
			break;

		
		case 1:
			Platform qq = ShareSDK.getPlatform(
					MyOtherSpaceActivity.this, QQ.NAME);
			if (qq.isValid()) {

			} else {
				qq.setPlatformActionListener(this);
				qq.SSOSetting(true);
				qq.showUser(null);

			}
			break;
		case 2:
			Platform facebook = ShareSDK.getPlatform(
					MyOtherSpaceActivity.this, Facebook.NAME);
			if (facebook.isValid()) {

			} else {
				facebook.setPlatformActionListener(this);
				facebook.SSOSetting(true);
				facebook.showUser(null);

			}
			break;
		case 5:
			Platform weixin = ShareSDK.getPlatform(
					MyOtherSpaceActivity.this, Wechat.NAME);
			if (weixin.isValid()) {

			} else {
				weixin.setPlatformActionListener(this);
				weixin.SSOSetting(true);
				weixin.showUser(null);
			}
			break;
		}
	}
	private void initList() {
		// TODO Auto-generated method stub
		list = new ArrayList<Integer>();
		Platform sinaweibo = ShareSDK.getPlatform(MyOtherSpaceActivity.this,
				SinaWeibo.NAME);
		if (sinaweibo.isValid()) {
			list.add(1);
		} else {
			list.add(0);
		}
		Platform qq = ShareSDK.getPlatform(MyOtherSpaceActivity.this, QQ.NAME);
		if (qq.isValid()) {
			list.add(1);
		} else {
			list.add(0);
		}
		Platform facebook = ShareSDK.getPlatform(MyOtherSpaceActivity.this,
				Facebook.NAME);
		if (facebook.isValid()) {
			list.add(1);
		} else {
			list.add(0);
		}
		Platform weixin = ShareSDK.getPlatform(MyOtherSpaceActivity.this,
				Wechat.NAME);
		if (weixin.isValid()) {
			list.add(1);
		} else {
			list.add(0);
		}
		// email
		// list.add(1);

		// private String[] text = { "新浪微博", "腾讯微博", "人人网", "邮箱", "手机",
		// "facebook",
		// "twitter", "微信",
		//
		// };

	}
	@Override
	public void onCancel(Platform arg0, int action) {
		// TODO Auto-generated method stub
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
		}
	}
	@Override
	public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
		// TODO Auto-generated method stub
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_COMPLETE, this);
			// login(platform.getName(), platform.getDb().getUserId(), res);
//			UserData = res;
//			if (platform.getName().equals("QZone")) {
//				// int b = platform.getId();
//				//    
//				// res.get("nickname"));
//				// Intent intent=new
//				// Intent(LoginActivity.this,TheThirdPartyActivity.class);
//				// intent.putE
//				// startActivity(intent);
//				//
//				QZone qz = new QZone(LoginActivity.this);
////				   
////
////				   
////						+ platform.getDb().getUserName());
//				if (qz.isValid()) {
//					disanfangLogin("qq", qz.getDb().getUserId(), qz
//							.getDb().getToken(), qz.getDb().getUserName());
//				}
//
//			} else if (platform.getName().equals("SinaWeibo")) {
//				SinaWeibo s = new SinaWeibo(LoginActivity.this);
//				//    
//				//    
//				//    
//				if (s.isValid())
//					disanfangLogin("sina", s.getDb().getUserId(),
//							s.getDb().getToken(), s.getDb()
//									.getUserName());
//			} else if (platform.getName().equals("Facebook")) {
//				Facebook fa = new Facebook(LoginActivity.this);
//				//    
//				//    
//				//    
//				if (fa.isValid())
//					disanfangLogin("facebook", fa.getDb().getUserId(),
//							fa.getDb().getToken(), fa.getDb()
//									.getUserName());
//
//			}

		}
	}
	@Override
	public void onError(Platform arg0, int action, Throwable arg2) {
		// TODO Auto-generated method stub
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);
		}
	}
	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		
		switch (msg.what) {
		case MSG_AUTH_CANCEL: {
			Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT)
					.show();
	     	}
		break;
	   case MSG_AUTH_ERROR: {
		Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT)
				.show();
   	}
		break;
	  case MSG_AUTH_COMPLETE: {
		Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT)
				.show();
		init();

	}
			break;
		}
		
		return false;
	}
 
}
